package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.EntBaseInfo;
import com.example.demo.entity.dto.EntBaseInfoDTO;
import com.example.demo.entity.vo.TycSearchVO;
import com.example.demo.forest.TycClient;
import com.example.demo.mapper.EntBaseInfoMapper;
import com.example.demo.service.EntBaseInfoService;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @ClassName EntBaseInfoServiceImpl
 * @Author liupanpan
 * @Date 2024/12/31
 * @Description
 */
@Service
public class EntBaseInfoServiceImpl extends ServiceImpl<EntBaseInfoMapper, EntBaseInfo> implements EntBaseInfoService {

    @Resource
    TycClient tycClient;
    @Value("${tyc.token}")
    private String token;
    @Value("${tyc.searchAPi}")
    private String searchAPi;

    @Override
    public List<EntBaseInfo> getEntListByTycNameSearch(List<String> customers) {
        if (customers == null || customers.isEmpty()) {
            return null;
        }
        List<EntBaseInfo> entBaseInfos = new ArrayList<>();
        customers.forEach(name -> {
            //获取天眼查搜索结果
            if (Objects.isNull(token) || Objects.isNull(searchAPi) || Objects.isNull(name)) {
                return ;
            }
            //请求url
            String result = tycClient.tycSearch(searchAPi,name,token);
            //将 JSON 字符串 result 转换为 TycSearchVO 类的实例
            TycSearchVO tycSearchVO = JSON.parseObject(result, TycSearchVO.class);
            if (Objects.isNull(tycSearchVO) || !Objects.equals(tycSearchVO.getError_code(),0)) {
                return ;
            }
            List<TycSearchVO.ResultItem> items = tycSearchVO.getResult().getItems();
//            List<TycSearchVO.ResultItem> items = tycRequest(name);
            if (Objects.nonNull(items)) {
                items.forEach(item -> {
                    //信用代码非空和非空白字符串
                    if (StringUtils.isNotBlank(item.getCreditCode())) {
                        EntBaseInfo entBaseInfo = EntBaseInfo.builder()
                                .entFullName(item.getName())
                                .uscc(item.getCreditCode())
                                .memo("手动触发接口添加企业数据，企业base:"+item.getBase())
                                .extendUrl("https://www.tianyancha.com/company/"+item.getId())
                                .status(item.getRegStatus().equals("注销") || item.getRegStatus().equals("吊销") ? 0 : 1)
                                .timestamp(new Date())
                                .build();
                        entBaseInfos.add(entBaseInfo);
                    }

                });
            }
        });
        return entBaseInfos;
    }
    //根据关键字匹配天眼查数据,封装后后返回
//    @Nullable
//    public List<TycSearchVO.ResultItem> tycRequest(String word) {
//        if (Objects.isNull(token) || Objects.isNull(searchAPi) || Objects.isNull(word)) {
//            return null;
//        }
//        //请求url
//        String result = tycClient.tycSearch(searchAPi, token, word);
//        //将 JSON 字符串 result 转换为 TycSearchVO 类的实例
//        TycSearchVO tycSearchVO = JSON.parseObject(result, TycSearchVO.class);
//        if (Objects.isNull(tycSearchVO) || !Objects.equals(tycSearchVO.getError_code(),0)) {
//            return null;
//        }
//        return tycSearchVO.getResult().getItems();
//    }
}
